package com.cleaningcloud.cld.mapper;

import com.cleaningcloud.cld.domain.CldProjectPost;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import com.cleaningcloud.cld.domain.vo.CldProjectPostVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 项目岗位Mapper接口
 *
 * @author cleaningcloud
 * @date 2024-10-15
 */
public interface CldProjectPostMapper {
    /**
     * 查询项目岗位
     *
     * @param projectPostId 项目岗位主键
     * @return 项目岗位
     */
    CldProjectPostSetVo selectCldProjectPostByProjectPostId(Long projectPostId);

    /**
     * 查询项目岗位列表
     *
     * @param cldProjectPost 项目岗位
     * @return 项目岗位集合
     */
    List<CldProjectPostSetVo> selectCldProjectPostList(CldProjectPostDto cldProjectPost);


    /**
     * 查询在岗员工
     *
     * @return
     */
    List<CldProjectPost> selectPostStaff(CldProjectPost cldProjectPost);


    /**
     * 项目管理 查询项目岗位
     *
     * @param cldProjectPostDto
     * @return
     */
    List<CldProjectPostSetVo> getProjectPostList(CldProjectPostDto cldProjectPostDto);


    /**
     * 根据项目id分组查询项目岗位
     *
     * @param projectId
     * @return
     */
    List<CldProjectPost> selectProjectPostGroupByByProjectId(Long projectId);


    /**
     * 根据项目id及员工id获取项目岗位信息及班制信息
     *
     * @param cldProjectPostDto
     * @return
     */
    CldProjectPostVo selectStaffPostClasses(CldProjectPostDto cldProjectPostDto);


    /**
     * 新增项目岗位
     *
     * @param cldProjectPost 项目岗位
     * @return 结果
     */
    int insertCldProjectPost(CldProjectPost cldProjectPost);


    int batchInsert(List<CldProjectPost> list);

    int batchUpdate(List<CldProjectPost> list);


    int batchProjectPostUpdate(List<CldProjectPost> list);

    /**
     * 修改项目岗位
     *
     * @param cldProjectPost 项目岗位
     * @return 结果
     */
    int updateCldProjectPost(CldProjectPost cldProjectPost);

    /**
     * 删除项目岗位
     *
     * @param projectPostId 项目岗位主键
     * @return 结果
     */
    int deleteCldProjectPostByProjectPostId(Long projectPostId);

    /**
     * 批量删除项目岗位
     *
     * @param projectPostIds 需要删除的数据主键集合
     * @return 结果
     */
    int deleteCldProjectPostByProjectPostIds(@Param("list") List<Long> list);


    /**
     * 查询班次是否与主职务设置一致
     * @param staffId
     * @param prijectId
     * @param classId
     * @return
     */
    @Select("select count(1) from cld_project_post where staff_id = #{staffId} and project_id = #{projectId} and classes_id = #{classId} and is_master_duits = 1")
    int selectMasterDuitsClass(@Param("staffId") Long staffId,@Param("projectId")Long prijectId,@Param("classId") Long classId);


    @Update("update cld_project_post set is_master_duits = 0 where project_id = #{projectId} and staff_id = #{staffId} ")
    void updateMasterDuitsByProjectIdAndStaffId(@Param("projectId") Long projectId, @Param("staffId") Long staffId);

    /**
     * 查询人员最大岗位id
     * @param projectId
     * @param staffId
     * @return
     */
    @Select("select project_post_id from cld_project_post where project_id = #{projectId} and staff_id = #{staffId} order by project_post_name asc limit 1")
    Long getProjectPostIdByOrderByPostNameAsc(@Param("projectId") Long projectId, @Param("staffId") Long staffId);

    @Update("update cld_project_post set is_master_duits = 1 where project_post_id = #{projectPostId} ")
    void updateSetMasterDuitsByProjectIdAndStaffId(@Param("projectPostId") Long projectPostId);

    /**
     * 根据项目id批量解绑在岗人员
     *
     * @param projectId
     */
    @Update("update cld_project_post set staff_id = null,classes_id = null,project_duits_name = null,is_master_duits = 0,is_class = 0  where project_id = #{projectId}")
    void allUnbindingJobByProjectId(@Param("projectId") Long projectId);


    /**
     * 根据项目岗位id解绑
     *
     * @param staffId
     */
    @Update("update cld_project_post set staff_id = null,project_post_no = REPLACE(project_post_no, SUBSTRING_INDEX(project_post_no, '-', -1), '暂无人员'),is_class = 0 where staff_id = #{staffId}")
    void unbindingPostByStaffId(@Param("staffId") Long staffId);

    /**
     * 根据项目岗位id解绑
     *
     * @param postId
     */
    @Update("update cld_project_post set staff_id = null,project_post_no = REPLACE(project_post_no, SUBSTRING_INDEX(project_post_no, '-', -1), '暂无人员'),is_class = 0  where project_post_id = #{postId}")
    void unbindingJobByPostId(@Param("postId") Long postId);


    // 查询员工是否在管理岗存在数据
    @Select("select count(1) from cld_project_post pp left join cld_project_groups pg on pg.project_groups_id = pp.project_groups_id where pp.staff_id = #{staffId} and pg.group_name = 0")
    int selectManagePost(@Param("staffId") Long staffId);


}
